package com.demo.demo.itext7;

/**
 * @author huangzhi
 * @ClassName HtmlToPdfUtils
 * @description:
 * @date 2022年10月11日 16:58
 */
import com.demo.demo.itext7.page.ChineseSplitterCharacters;
import com.demo.demo.itext7.page.FooterEventHandler;
import com.demo.demo.itext7.page.NumberEventHandler;
import com.demo.demo.itext7.page.PageEventHandler;
import com.demo.demo.itext7.page.HeaderEventHandler;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.IBlockElement;
import com.itextpdf.layout.element.IElement;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.font.FontProvider;

import com.itextpdf.layout.font.FontSet;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.splitting.ISplitCharacters;
import com.lowagie.text.pdf.BaseFont;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
@Slf4j
public class HtmlToPdfUtils {

    public static void main(String[] args) throws IOException {
        PdfFont font2 = null;
      /*  try {
            font2 = PdfFontFactory.createFont("D:\\font\\MYingHei.ttf", PdfEncodings.IDENTITY_H, true);
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        Image image = null;
       /* try {

            image = new Image(ImageDataFactory.create("D:\\output-onlineimagetools.jpg"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }*/

        FontSet fontSet=new FontSet();
        fontSet.addFont("D:\\font");
        File file = new File("D:\\hello.html");
        FileInputStream in =new FileInputStream(file);

        File outF = new File("D:\\hello2.pdf");
        FileOutputStream out =new FileOutputStream(outF);

        String waterMarkText =  "自定义水印";
        convertToPdf(font2,image,in,waterMarkText,"D:\\font\\MYingHei.ttf",out);
    }

    /**
     * html转pdf
     *
     * @param  inputStream 输入流
     * @param  waterMark 水印
     * @param fontPath 字体路径，ttc后缀的字体需要添加<b>,0<b/>
     * @param  outputStream 输出流
     * @date : 2021/1/15 14:07
     */
    public static void convertToPdf(PdfFont font2,Image image,InputStream inputStream, String waterMark, String fontPath, OutputStream outputStream) throws IOException {
        Document document = null;

           String htmlStr = IOUtils.toString(inputStream, String.valueOf(StandardCharsets.UTF_8));

           PdfWriter pdfWriter = new PdfWriter(outputStream);
           PdfDocument pdfDocument = new PdfDocument(pdfWriter);
           //设置为A4大小
           //pdfDocument.setDefaultPageSize(PageSize.A4);
           //添加水印
           // pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new PageEventHandler());
          // pdfDocument.addEventHandler(PdfDocumentEvent.START_PAGE, new HeaderEventHandler(image));
           //pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new FooterEventHandler(font2));
           pdfDocument.addEventHandler(PdfDocumentEvent.START_PAGE, new HeaderEventHandler());
           pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new FooterEventHandler());
          pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new NumberEventHandler());

           document = new Document(pdfDocument, PageSize.A4);
           document.setProperty(Property.SPLIT_CHARACTERS, (ISplitCharacters) (text, glyphPos) ->  true);
           document.setMargins(50, 45, 50, 45);

        document.setSplitCharacters(new ChineseSplitterCharacters());
           ConverterProperties c = new ConverterProperties();
           c.setCharset("utf-8");
        /*   FontProvider defaultFontProvider = new FontProvider();
        defaultFontProvider.addFont("D:\\MYingHei_PRC_W4.ttf", BaseFont.IDENTITY_H);
        c.setFontProvider(defaultFontProvider);*/
           List<IElement> list = HtmlConverter.convertToElements(htmlStr, c);
           for (IElement ie : list) {
               document.add((IBlockElement) ie);
           }
           //添加中文字体支持
           ConverterProperties properties = new ConverterProperties();


       /* PdfFont sysFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
        fontProvider.addFont(sysFont.getFontProgram(), "UniGB-UCS2-H");*/
          /* fontProvider.addFont("D:\\font\\MYingHei.ttf", BaseFont.IDENTITY_H);

           //添加自定义字体，例如微软雅黑
           if (StringUtils.isNotBlank(fontPath)){
               PdfFont microsoft = PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H, false);
               fontProvider.addFont(microsoft.getFontProgram(), PdfEncodings.IDENTITY_H);
           }

           properties.setFontProvider(fontProvider);*/
           // HtmlConverter.convertToPdf(inputStream, pdfDocument, properties);

           // pdfWriter.close();
           // pdfDocument.close();
        if (document != null) {
            document.flush();
            document.close();
        }


    }

}